<?php

namespace addons\hlapp\controller;

use addons\hlapp\dto\Result;
use addons\hlapp\model\HlappMember;
use addons\hlapp\model\HlappPart;
use addons\hlapp\model\HlappProject;
use addons\hlapp\model\HlappProjectEvaluate;
use addons\hlapp\model\HlappProjectSolve;
use addons\hlapp\model\HlappProjectStatus;
use think\Db;
use think\Exception;

class Solve extends Common
{
    public $adminLogin = true;//注意：因为这是后台业务菜单所以需要管理员登录操作的
    public $pageSize = 10;
    public static $SolveColumn= array(
        "项目名称"=>"p_name",
        "阶段"=>"stage",
        "周数"=>"week",
        "甲方评价人"=>"m_name",
        "评价总状态"=>"status",
        "评价详情"=>"evaluateInfo",
        "评价详情"=>"evaluateInfo",
        "评价时间"=>"time",
    );

//-------------
//---- 页面
//-------------


    /**
     * 分页
     */
    public function pagelist()
    {
        $name = $this->request->param("name");
        $stage = $this->request->param("stage");
        $week = $this->request->param("week");
        $e = new HlappProjectEvaluate();
        $s =  new HlappProjectSolve();
        //查询条件：项目名称
        if ($name){
            $pids = "";
            $eids = "";
            $plist = (new HlappProject())->where('name','like','%'.$name.'%')->select();
            foreach ($plist as $p){
                $pids= $pids.','.$p['id'];
            }
            $evaluates = $e->where('projectId','in',$pids)->select();
            foreach ($evaluates as $ev){
                $eids= $eids.','.$ev['id'];
            }
            $s = $s->where('eId','in',$eids);
        }
        if($stage != null && $stage != ""){
            $s = $s->where('e.stage',$stage);
        }
        if($week != null && $week != ""){
            $s = $s->where('e.week',$week);
        }

        $list = $s->field("hl_hlapp_project_solve.*,
         e.create_time e_ctime,e.week week,e.stage stage,
         p.create_time p_ctime,p.name p_name,p.engineer_id,p.manager_id,p.operate_id,p.collection_id,p.engineering_id")
            ->leftJoin("hlapp_project_evaluate e","e.id = hl_hlapp_project_solve.eId")
            ->leftJoin('hlapp_project p','p.id = e.projectId')
            ->order('id','desc')
            ->paginate($this->pageSize,false,['query'=>request()->param()]);
/*        for ($i = 0;$i < count($list);$i++){
            $weeks = $this->getWeeksBystamp($list[$i]['p_ctime'],$list[$i]['e_ctime']);
            $list[$i]['weeks'] = $weeks;
        }*/
        $page = request()->param('page');
        $page || ($page=1);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('name',$name);
        return $this->fetch();
    }

    /**
     * 删除方法
     */
    public function delete(){
        $r = Result::getError();
        try{
            $id = $_POST['id'];
            $p = HlappProject::get($id);
            $p->together('rolesMember')->delete();
            $r = Result::getOk();
        }catch (Exception $e){

        }
        return $r;
    }

    /**
     * 评价方案
     */
    public function evaluateSolveList(){
        $eId = $_GET['eId'];
        $list=(new HlappProjectSolve())->with('partInfo')->where('eId',$eId)->select();
        $this->assign("list",$list);
        $this->fetch();
    }

    /**
     * 导出表格
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exportExl(){
        $name = $this->request->param("name");
        $stage = $this->request->param("stage");
        $week = $this->request->param("week");
        $whereSql = "";
        $e = new HlappProjectEvaluate();
        $s =  new HlappProjectSolve();
        //查询条件：项目名称
        if ($name){
            $pids = "";
            $eids = "";
            $plist = (new HlappProject())->where('name','like','%'.$name.'%')->select();
            foreach ($plist as $p){
                $pids= $pids.','.$p['id'];
            }
            $evaluates = $e->where('projectId','in',$pids)->select();
            foreach ($evaluates as $ev){
                $eids= $eids.','.$ev['id'];
            }
            $s = $s->where('eId','in',$eids);
        }
        if($stage != null && $stage != ""){
            $whereSql = $whereSql." e.stage = ".$stage;
        }
        if($week != null && $week != ""){
            $whereSql = $whereSql." e.week = ".$week;
        }

        $sql="select 
                case e.`status`
                    when 1 then '满意'
                    when 0 then '不满意'
                end 'status',
                case e.stage
                    when 0 then '样板房'
                    when 1 then '前期进场'
                    when 2 then '前期施工'
                    when 3 then '施工过程'
                    when 4 then '施工收尾'
                    when 5 then '售后维修'
                end 'stage',
                m.name 'm_name',
                e.week 'week',
                p.name 'p_name',
                (
                select 
                REPLACE(REPLACE(REPLACE(e.evaluateInfo,'{\"key\":\"',\"\"),'\",\"value\"',\"\"),'}','')
                from hl_hlapp_project_evaluate ee where ee.id = e.id
                ) 'evaluateInfo',
                from_unixtime(e.update_time,'%Y-%m-%d') 'time' 
                from hl_hlapp_project_evaluate e
                left join hl_hlapp_project p on p.id = e.projectId
                left join hl_hlapp_member m on m.id = p.amamager_id
                where e.delete_time is null
                ".$whereSql."
                order by e.id desc
                ";
        $list = Db::query($sql);

        $path=$this->exportExcel('evaluate',self::$EvaluateColumn,$list);
        return Result::getOk($path);
    }
}